package com.system.runtai.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.system.common.result.Result;
import com.system.runtai.entity.ArticleCollect;
import com.system.runtai.entity.CompanyCase;
import com.system.runtai.entity.CompanyDynamic;
import com.system.runtai.entity.SecondaryCopywriter;
import com.system.runtai.service.ArticleCollectService;
import com.system.runtai.service.CompanyCaseService;
import com.system.runtai.service.SecondaryCopywriterService;
import com.system.runtai.utils.AuthContextUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 公司案例 前端控制器
 * </p>
 *
 * @author runtai
 * @since 2024-03-29
 */
@Api(tags = "公司案例")
@RestController
@RequestMapping("/runtai/company-case")
public class CompanyCaseController {


    @Autowired
    private CompanyCaseService companyCaseService;

    @Autowired
    private SecondaryCopywriterService secondaryCopywriterService;

    @Autowired
    private ArticleCollectService articleCollectService;

//    @ApiOperation(value = "润泰公告二级文案标题筛选")
//    @GetMapping("/getSecondaryCopywriterfilter")
//    public Result<List<SecondaryCopywriter>> getSecondaryCopywriterfilter(@RequestParam(required = false) String title){
//        LambdaQueryWrapper<SecondaryCopywriter> wrapper = new LambdaQueryWrapper<>();
//        wrapper.like(SecondaryCopywriter::getTitle,title);
//        List<SecondaryCopywriter> list = secondaryCopywriterService.list(wrapper);
//
//        return Result.ok(list);
//    }

    @ApiOperation(value = "公司案例二级文案")
    @GetMapping("/getSecondaryCopywriter/{id}")
    public Result<List<SecondaryCopywriter>> getSecondaryCopywriter(@PathVariable Integer id,@RequestParam(required = false) String title){
        if (StringUtils.isEmpty(title)) {
            LambdaQueryWrapper<SecondaryCopywriter> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(SecondaryCopywriter::getStartid, 4).eq(SecondaryCopywriter::getMainArgumentId, id);
            List<SecondaryCopywriter> list = secondaryCopywriterService.list(wrapper);
            return Result.ok(list);
        }else if (!StringUtils.isEmpty(title)){
            LambdaQueryWrapper<SecondaryCopywriter> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(SecondaryCopywriter::getStartid, 4).eq(SecondaryCopywriter::getMainArgumentId, id).like(SecondaryCopywriter::getTitle,title);
            List<SecondaryCopywriter> list = secondaryCopywriterService.list(wrapper);
            return Result.ok(list);
        }

        List<SecondaryCopywriter> list = null;
        return Result.fail(list).message("对象为空");
    }




    @ApiOperation(value = "添加公司案例二级文案")
    @PostMapping("/saveSecondaryCopywriter/{id}")
    public Result<SecondaryCopywriter> saveSecondaryCopywriter(@PathVariable Integer id,@RequestBody SecondaryCopywriter secondaryCopywriter){

        secondaryCopywriter.setCreateTime(LocalDateTime.now());
        secondaryCopywriter.setStartid(4);
        secondaryCopywriter.setMainArgumentId(id);
        boolean save = secondaryCopywriterService.save(secondaryCopywriter);

        if (save){
            return Result.ok(secondaryCopywriter).message("添加成功");
        }
        return Result.fail(secondaryCopywriter).message("添加失败");
    }


    @ApiOperation(value = "修改二级文案")
    @PostMapping("/updateSecondaryCopywriter")
    public Result<SecondaryCopywriter> updateSecondaryCopywriter(@RequestBody SecondaryCopywriter secondaryCopywriter){

        secondaryCopywriter.setUpdateTime(LocalDateTime.now());

        boolean b = secondaryCopywriterService.updateById(secondaryCopywriter);

        if (b){
            return Result.ok(secondaryCopywriter).message("修改成功");
        }
        return Result.fail(secondaryCopywriter).message("修改失败");
    }

    @ApiOperation(value = "删除二级文案")
    @PostMapping("delectSecondaryCopywriter/{id}")
    public Result delectSecondaryCopywriter(@PathVariable Integer id){

        boolean b = secondaryCopywriterService.removeById(id);

        if (b){
            return Result.ok().message("删除成功");
        }
        return Result.fail().message("删除失败");
    }


    @ApiOperation(value = "所有公司案例")
    @GetMapping("/companyCaseList")
    public Result<List<CompanyCase>> companyCaseList() {
        List<CompanyCase> list = companyCaseService.list();
        return Result.ok(list);
    }

    @ApiOperation(value = "后台公司案例详情")
    @GetMapping("/companyCaseht/{id}")
    public Result<CompanyCase> companyCaseByIdht(@PathVariable Integer id) {
        CompanyCase byId = companyCaseService.getById(id);
        return Result.ok(byId);
    }

    @ApiOperation(value = "程序公司案例详情")
    @GetMapping("/companyCasecx/{id}")
    public Result<CompanyCase> companyCaseByIdcx(@PathVariable Integer id) {
        CompanyCase byId = companyCaseService.getById(id);

        String s = AuthContextUtil.get();
        LambdaQueryWrapper<ArticleCollect> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ArticleCollect::getAppletUserId, Integer.valueOf(s)).eq(ArticleCollect::getStartId, 4).eq(ArticleCollect::getInRegardOfId, id);
        ArticleCollect one = articleCollectService.getOne(wrapper);

        if (one == null) {
            byId.setCollect(0);
            return Result.ok(byId);
        } else {
            byId.setCollect(1);
            return Result.ok(byId);
        }
    }

    @ApiOperation(value = "查看所有公司案例一、二级文案")
    @GetMapping("/getCompanyCaseSC/{startId}")
    public Result<IPage<CompanyCase>> getCompanyCaseList(@PathVariable Integer startId, @RequestParam(required = false) Long page, @RequestParam(required = false) Long limit) {
        List<CompanyCase> list = companyCaseService.list();

        Page<CompanyCase> recommendPage = new Page<>(page, limit);

        LambdaQueryWrapper<CompanyCase> wrapper = new LambdaQueryWrapper<>();
        Integer id = null;
        for (CompanyCase companyCase : list) {
            id = companyCase.getId();
        }

        if (id != null) {
            wrapper.eq(CompanyCase::getId, id).orderByDesc(CompanyCase::getCreateTime);
        }

        IPage<CompanyCase> iPage = companyCaseService.page(recommendPage, wrapper);
        for (CompanyCase record : iPage.getRecords()) {
            LambdaQueryWrapper<SecondaryCopywriter> wrapper1 = new LambdaQueryWrapper<>();
            wrapper1.eq(SecondaryCopywriter::getStartid, startId).eq(SecondaryCopywriter::getMainArgumentId, record.getId());
            List<SecondaryCopywriter> copywriterList = secondaryCopywriterService.list(wrapper1);
            record.setSecondaryCopywriterList(copywriterList);
        }

        return Result.ok(iPage);
    }


//    @ApiOperation(value = "查看所有公司案例一、二级文案")
//    @GetMapping("/getCompanyCaseSC")
//    public Result<List<CompanyCase>> getCompanyCaseList(){
//        List<CompanyCase> list = companyCaseService.list();
//        for (int i = 0; i < list.size(); i++) {
//            LambdaQueryWrapper<SecondaryCopywriter> wrapper = new LambdaQueryWrapper<>();
//            wrapper.eq(SecondaryCopywriter::getStartid,list.get(i).getId()).eq(SecondaryCopywriter::getMainArgumentId,list.get(i).getId());
//            List<SecondaryCopywriter> copywriterList = secondaryCopywriterService.list(wrapper);
//            list.get(i).setSecondaryCopywriterList(copywriterList);
//        }
//        return Result.ok(list);
//    }

    @ApiOperation(value = "添加公司案例")
    @PostMapping("/saveCompanyCase")
    public Result<CompanyCase> saveCompanyCase(@RequestBody CompanyCase companyCase) {

        companyCase.setCreateTime(LocalDateTime.now());
        boolean save = companyCaseService.save(companyCase);

        if (save) {
            return Result.ok(companyCase).message("添加成功");
        } else {
            return Result.ok(companyCase).message("添加失败");
        }
    }


    @ApiOperation(value = "修改公司案例")
    @PostMapping("/updateCompanyCase")
    public Result<CompanyCase> updateCompanyCase(@RequestBody CompanyCase companyCase) {

        companyCase.setUpdateTime(LocalDateTime.now());
        boolean update = companyCaseService.updateById(companyCase);

        if (update) {
            return Result.ok(companyCase).message("修改成功");
        } else {
            return Result.ok(companyCase).message("修改失败");
        }
    }


    @ApiOperation(value = "删除公司案例")
    @PostMapping("delectCompanyCase/{id}")
    public Result delectCompanyCase(@PathVariable Integer id) {

        boolean b = companyCaseService.removeById(id);

        if (b) {
            return Result.ok().message("删除成功");
        }
        return Result.fail().message("删除失败");
    }

    @ApiOperation(value = "分页查询公司案例")
    @GetMapping("/{page}/{limit}")
    public Result<IPage<CompanyCase>> index32(@PathVariable Long page, @PathVariable Long limit, CompanyCase companyCase) {

        Page<CompanyCase> recommendPage = new Page<>(page, limit);

        LambdaQueryWrapper<CompanyCase> wrapper = new LambdaQueryWrapper<>();

        Integer id = companyCase.getId();

        if (id != null) {
            wrapper.eq(CompanyCase::getId, id);
        }

        IPage<CompanyCase> iPage = companyCaseService.page(recommendPage, wrapper);

        return Result.ok(iPage);
    }


    @ApiOperation(value = "收藏公司案例：4")
    @PostMapping("collectCompanyCase")
    public Result collectCompanyCase(@RequestBody CompanyCase companyCase) {

        String s = AuthContextUtil.get();
        LambdaQueryWrapper<ArticleCollect> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ArticleCollect::getInRegardOfId, companyCase.getId()).eq(ArticleCollect::getAppletUserId, Integer.valueOf(s)).eq(ArticleCollect::getStartId,4);
        ArticleCollect one = articleCollectService.getOne(wrapper);

        if (one!=null){
            return Result.ok(one);
        }else {
            ArticleCollect articleCollect = new ArticleCollect();
            articleCollect.setCreateTime(LocalDateTime.now());
            articleCollect.setAppletUserId(Integer.valueOf(s));
            articleCollect.setInRegardOfId(companyCase.getId());
            articleCollect.setStartId(4);
            boolean b = articleCollectService.save(articleCollect);

            if (b) {
                return Result.ok().message("添加收藏成功");
            }
            return Result.fail().message("添加收藏失败");
        }
    }

    @ApiOperation(value = "删除公司案例：4收藏")
    @PostMapping("delectCompanyCaseCollect")
    public Result delectCompanyCaseCollect(@RequestBody CompanyDynamic companyDynamic) {
        String s = AuthContextUtil.get();

        LambdaQueryWrapper<ArticleCollect> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ArticleCollect::getInRegardOfId, companyDynamic.getId()).eq(ArticleCollect::getAppletUserId, Integer.valueOf(s)).eq(ArticleCollect::getStartId,4);
        boolean b = articleCollectService.removeById(wrapper);

        if (b) {
            return Result.ok().message("删除成功");
        }
        return Result.fail().message("删除失败");
    }

}

